package com.bw.park.ui.login

import android.text.TextUtils
import android.util.Log
import androidx.databinding.ObservableField
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.ToastUtils
import com.bw.park.entity.LoginEntity
import com.mooc.lib_common.bus.LiveDataBus
import com.mooc.lib_network.entity.NetResult
import kotlinx.coroutines.launch
import org.json.JSONObject


class LoginViewModel:ViewModel() {

    val loginRepo = LoginRepo()
    val name = ObservableField<String>()
    val pwd = ObservableField<String>()
    val loginBus = LiveDataBus<String>()

    fun onLoginClick() {
        if (TextUtils.isEmpty(name.get())){
            ToastUtils.showShort("请输入用户名")
            return
        }
        if (TextUtils.isEmpty(pwd.get())){
            ToastUtils.showShort("请输入密码")
            return
        }
        viewModelScope.launch {
            var requestStr = "{\"uName\":\""+name.get()+"\"," +
                    "\"uPwd\":\""+pwd.get()+"\"," +
                    "\"time\":\""+(System.currentTimeMillis()/1000)+"\"}"
            val result = loginRepo.login(requestStr)
            if (result is NetResult.Success){
                //成功
                loginBus.postValue("http://118.195.161.134:8088/index.html")
                Log.e("ZXY","NetResult.Success")
            }else{
                Log.e("ZXY","NetResult.Erro")
            }
        }
    }
}